package net.biz.reminder;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import net.biz.reminder.util.JDBCOracleUtil;
import net.biz.reminder.util.PropertyUtil;

public class MailSender implements Runnable {
	private String ID = "";

	public MailSender(String id) {
		ID = id;
	}

	public void run() {
		// 1.查询数据
		try {
			System.out.println("执行任务ID的发送邮件线程:" + ID);
			List<Map<String, Object>> result = JDBCOracleUtil
					.executeQuery("SELECT * FROM ADM_REMIND WHERE ID=" + ID
							+ " AND VALID='1'");
			if (result.size() == 1
					& String.valueOf(result.get(0).get("RESULT")).length() > 1) {
				String[] mails = String.valueOf(result.get(0).get("MAIL"))
						.split(",");
				// 2.发送邮件
				String username = PropertyUtil.get("mail.user");
				String password = PropertyUtil.get("mail.password");
				String server = PropertyUtil.get("mail.smtp.host");

				Properties prop = new Properties();
				// 认证信息
				Authenticator auth = new EMailAutherticator(username, password);
				prop.put("mail.smtp.host", server);
				prop.put("mail.smtp.auth", "true");
				Session session = Session.getDefaultInstance(prop, auth);
				MimeMessage message = new MimeMessage(session);
				// 设置主题
				message.setSubject(PropertyUtil.get("mail.subject")
						+ String.valueOf(result.get(0).get("SUBJECT")));
				// 设置简单文本内容
				//message.setText(String.valueOf(result.get(0).get("RESULT_D")));
				//设置HTML 格式的邮件：
				message.setContent(String.valueOf(result.get(0).get("RESULT_D")), "text/html;charset = utf-8");  
				// 设置发送日期
				message.setSentDate(new Date());
				// 设置头
				message.setHeader("this is head of this mail",
						"this is head of this mail");
				// 设置from
				Address address = new InternetAddress(username, "提醒邮件");
				message.setFrom(address);
				// 设置to
				Address[] tos = null;
				if (mails != null) {
					tos = new InternetAddress[mails.length + 1];
					tos[0] = new InternetAddress(mails[0]);
					for (int i = 0; i < mails.length; i++) {
						tos[i + 1] = new InternetAddress(mails[i]);
					}
				}
				message.setRecipients(Message.RecipientType.TO, tos);
				
				//测试用 begin
//				message.saveChanges();  
//				message.writeTo(new FileOutputStream(ID+"textMail.eml"));
				//测试用 end
				
				// 发送邮件
				Transport.send(message);
				System.out.println("Mail send is ok!  "+String.valueOf(result.get(0).get("MAIL")));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}
	

}
